import { Component, OnInit } from '@angular/core';
import {CONSTANT} from "../../service/constant.service";
import {ROUTE} from "../../service/route.service";
import {LocalStorage} from "../../app.local-storage";
import {CONTRACT} from "../../service/contract.service";
import {PAYMENT} from "../../service/payment.service";
import {WfToast} from "../../utils/toast.utils";
import {ActivatedRoute, Params} from "@angular/router";
import {CheckStandModel} from "../../model/checkstand.model";
import {UserModel} from "../../model/user.model";
import {USER} from "../../service/user.service";
declare var $:any;

@Component({
  selector: 'app-confirm-payment-view',
  templateUrl: './confirm-payment-view.component.html',
  styleUrls: ['./confirm-payment-view.component.scss']
})
export class ConfirmPaymentViewComponent implements OnInit {

  progressBarList:any=this.constant.progressBarList;
  checkstand:CheckStandModel;
  user:UserModel;

  constructor(private constant:CONSTANT,
              private route:ROUTE,
              private contract:CONTRACT,
              private paymentApi:PAYMENT,
              private toast:WfToast,
              private USER:USER,
              private ls:LocalStorage,
              private routeInfo:ActivatedRoute) {
    this.checkstand = this.ls.getObject('checkstand') as CheckStandModel;
    if(this.checkstand.isNormal){
      this.progressBarList = [];
    }
    this.user = this.USER.get();
  }
  payType:any={
    alipay:2,
    weChat:1,
    balance:0,
  }
  payment(){
    let value:any = $('input[name=pay]:checked').val();
    if(value!=1&&value!=2&&value!=0){
      this.toast.msg('请选择支付方式');
      return;
    }
    if(value==2){
      let reqData:any={
        billNo:this.checkstand.billNo,
        returnUrl:encodeURIComponent(`http://qgy-pc.test.ostengar.com/#/finished_payment`)
      }
      this.paymentApi.getAlipayPayment(reqData,(res:any)=>{
        window.location.href=res;
      })
    }else if(value==1){
      let reqData:any={
        billNo:this.checkstand.billNo
      }
      this.paymentApi.getWeChatPayment(reqData,(res:any)=>{
        this.route.goWechatPaymentView(res);
      })
    }else{
      let reqData:any={
        orderNo:this.checkstand.billNo,
        money:this.checkstand.money
      }
      this.paymentApi.getBalancePayment(reqData,(res:any)=>{
        this.route.goFinishedPaymentView();
      })
    }
  }
  ngOnInit() {
    this.routeInfo.params.subscribe((param:any)=>{
      this.checkstand = this.ls.getObject('checkstand') as CheckStandModel;
    });
  }

  goToFinishedPaymentView(){
    this.payment();
  }
}
